
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Linux运维文档之nginx【转】 | 安生</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1.检查并且安装依赖组件检查安装nginx的依赖性，nginx的模块需要第三方库的支持，检查是否安装下列库：zlib、zlib-devel、openssl、openssl-devel、prce、prce-devel如果没有，则全部装上
yum install zlib zlib-devel openssl openssl-devel prce prce-devel2.安装pcre
`&amp;lt;/">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux运维文档之nginx【转】">
<meta property="og:url" content="http://localhost/2014/12/09/Linux运维文档之nginx【转】/index.html">
<meta property="og:site_name" content="安生">
<meta property="og:description" content="1.检查并且安装依赖组件检查安装nginx的依赖性，nginx的模块需要第三方库的支持，检查是否安装下列库：zlib、zlib-devel、openssl、openssl-devel、prce、prce-devel如果没有，则全部装上
yum install zlib zlib-devel openssl openssl-devel prce prce-devel2.安装pcre
`&amp;lt;/">
<meta property="og:updated_time" content="2016-04-11T05:46:51.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux运维文档之nginx【转】">
<meta name="twitter:description" content="1.检查并且安装依赖组件检查安装nginx的依赖性，nginx的模块需要第三方库的支持，检查是否安装下列库：zlib、zlib-devel、openssl、openssl-devel、prce、prce-devel如果没有，则全部装上
yum install zlib zlib-devel openssl openssl-devel prce prce-devel2.安装pcre
`&amp;lt;/">
  
    <link rel="alternative" href="/atom.xml" title="安生" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.min.js"></script><![endif]-->
  
</head>
<body>
<div id="container">
  <div id="wrap">
    <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">安生</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="http://www.baidu.com/baidu" method="get" accept-charset="utf-8" class="search-form">
          <input type="search" name="word" maxlength="20" class="search-form-input" placeholder="Search">
          <input type="submit" value="" class="search-form-submit">
          <input name=tn type=hidden value="bds">
          <input name=cl type=hidden value="3">
          <input name=ct type=hidden value="2097152">
          <input type="hidden" name="si" value="localhost">
        </form>
      </div>
    </div>
  </div>
</header>
    <div class="outer">
      <section id="main"><article id="post-Linux运维文档之nginx【转】" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/12/09/Linux运维文档之nginx【转】/" class="article-date">
  <time datetime="2014-12-08T21:58:00.000Z" itemprop="datePublished">2014-12-09</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Linux运维文档之nginx【转】
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>  1.检查并且安装依赖组件<br>检查安装nginx的依赖性，nginx的模块需要第三方库的支持，检查是否安装下列库：zlib、zlib-devel、openssl、openssl-devel、prce、prce-devel如果没有，则全部装上</p>
<pre><code>yum install zlib zlib-devel openssl openssl-devel prce prce-devel2.安装pcre
`&lt;/pre&gt;

&lt;pre&gt;`wget http://xiyoulinux.qiniudn.com/pcre-8.36.tar.gz
tar -xzvf pcre-8.36.tar.gz -C ../software/
cd ../software/pcre-8.36/
./configure
make &amp;amp;&amp;amp; make install
`&lt;/pre&gt;

  3.安装google-perftools

&lt;pre&gt;`wget http://xiyoulinux.qiniudn.com/libunwind-1.1.tar.gz     
wget http://xiyoulinux.qiniudn.com/gperftools-2.3.tar.gz
tar -xzvf libunwind-1.1.tar.gz -C ../software/
tar -xzvf gperftools-2.3.tar.gz -C ../software/
cd ../software/llibunwind-1.1/
./configure
make
make install
cd ../gperftools-2.3/
./configure
make &amp;amp;&amp;amp; make install
`&lt;/pre&gt;

4.编译安装NGINX

先建nginx的启动用户

useradd –s /sbin/nologin nginx

wget http://nginx.org/download/nginx-1.7.8.tar.gz

tar -xzvf nginx-1.7.8.tar.gz -C ../software/

cd ../software/nginx-1.7.8/

&lt;pre&gt;`./configure –with-cc-opt=’-O3′ \ # 注意整个不是零，是大写英文字母O&amp;amp;nbsp;
–with-google_perftools_module \ # 可选组件
–prefix=/usr/local/nginx \ # nginx安装目录
–with-openssl=/usr/lib \
–with-http_stub_status_module \
–with-http_image_filter_module \
–user=nginx \
–group=nginx

make &amp;amp;&amp;amp; make install修改一下配置：

grep nginx /usr/local/nginx/conf/nginx.conf

user nginx nginx;要点：禁止DEBUG模式
</code></pre><h1 id="vi-auto-cc-gcc"><a href="#vi-auto-cc-gcc" class="headerlink" title="vi auto/cc/gcc"></a>vi auto/cc/gcc</h1><h1 id="debug-注释下面"><a href="#debug-注释下面" class="headerlink" title="debug //注释下面"></a>debug //注释下面</h1><p>CFLAGS=”$CFLAGS -g”</p>
<p><span style="font-size:16px;">5.nginx的信号控制(有关nginx的启动与关闭)</span></p>
<p>TERM,INT 快速关闭QUIT 从容关闭</p>
<p>HUP 平滑重启，重新加载配置文件</p>
<p>USR1 重新打开日志文件，在切割日志时用途较大；</p>
<p>USR2 平滑升级可执行程序</p>
<p>WINCH 从容关闭工作进程</p>
<p>我们可以直接通过以下命令来完成平滑重启，省下寻找nginx主进程号的步骤；</p>
<p>kill -“信号类型”‘/usr/local/nginx/logs/nginx.pid’或者nginx的主进程号</p>
<p>(1）、从容停止nginx</p>
<p>kill -QUIT 6019 #nginx主进程号</p>
<p>kill -QUIT <code>cat /usr/local/nginx/logs/nginx.pid</code></p>
<p>(2)、快速停止nginx</p>
<p>kill -TERM /INT nginx主进程号</p>
<p>kill -TERM /INT `cat /usr/local/nginx/logs/nginx.pid</p>
<p>(3)、强制停止所有的nginx进程</p>
<p>pkill -9 nginx</p>
<p>(4)、平滑重启nginx</p>
<p>修改了nginx的配置文件要重启nginx；重启之前要检查配置文件是否正确：</p>
<h1 id="usr-local-nginx-sbin-nginx-t-c-usr-local-nginx-conf-nginx-conf"><a href="#usr-local-nginx-sbin-nginx-t-c-usr-local-nginx-conf-nginx-conf" class="headerlink" title="/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf"></a>/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf</h1><p>the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok</p>
<p>configuration file /usr/local/nginx/conf/nginx.conf test is successful</p>
<h1 id="kill-HUP-cat-usr-local-nginx-logs-nginx-pid"><a href="#kill-HUP-cat-usr-local-nginx-logs-nginx-pid" class="headerlink" title="kill -HUP cat /usr/local/nginx/logs/nginx.pid"></a>kill -HUP <code>cat /usr/local/nginx/logs/nginx.pid</code></h1><p>nginx的平滑重启</p>
<p>kll HUP Nginx主进程号</p>
<p>或者</p>
<p>kll HUP nginx.pid文件按存放路径•</p>
<p>  <span style="color:#555555;font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">NGINX基本配置</span><br><span style="font-size:12px;">Nginx.conf配置文件：<br>//以下为配置内容<br>user nginx; # 指定运行nginx的用户和组<br>worker_processes 2; # 工作进程数,基本为CPU的核心数或者两倍</span></p>
<h1 id="指定全局错误日志的路径，错误日志可选项-有-debug-info-notice-warn-error-crit"><a href="#指定全局错误日志的路径，错误日志可选项-有-debug-info-notice-warn-error-crit" class="headerlink" title="指定全局错误日志的路径，错误日志可选项 有[debug|info|notice|warn|error|crit]"></a>指定全局错误日志的路径，错误日志可选项 有[debug|info|notice|warn|error|crit]</h1><p>error_log logs/error.log info;<br>pid logs/nginx.pid; # 指定pid文件位置<br>events {<br>worker_connections 1024; # 最大连接数<br>}<br>http {<br>include mime.types; # 设定mime类型<br>default_type application/octet-stream;<br>//定义日志格式</p>
<p>#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘</p>
<h1 id="‘-status-body-bytes-sent-“-http-referer”-‘"><a href="#‘-status-body-bytes-sent-“-http-referer”-‘" class="headerlink" title="‘$status $body_bytes_sent “$http_referer” ‘"></a>‘$status $body_bytes_sent “$http_referer” ‘</h1><h1 id="‘”-http-user-agent”-“-http-x-forwarded-for”‘"><a href="#‘”-http-user-agent”-“-http-x-forwarded-for”‘" class="headerlink" title="‘”$http_user_agent” “$http_x_forwarded_for”‘;"></a>‘”$http_user_agent” “$http_x_forwarded_for”‘;</h1><p>access_log logs/access.log main; # 访问日志目录以及格式<br>sendfile on; # sendfile有效提高web文件传输速度</p>
<p>#tcp_nopush on;</p>
<p>#keepalive_timeout 0;<br>keepalive_timeout 65;<br>gzip on;</p>
<p>#站点配置<br>server {<br>listen 80;<br>server_name localhost;</p>
<p>#charset koi8-r;</p>
<p>#access_log logs/host.access.log main;<br>//定义主目录,类似apache的DocumentRoot<br>location / {<br>root /nginx_www; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html; # 将500错误转到50x.html上<br>location = /50x.html { # 如果访问的页面等于50x.html,则从html目录下找<br>root /nginx_www;<br>}<br>}<br>}<br>首先有个全局的配置<br>然后配置一个httpd段<br>httpd配置段里面包含多个server段,也就是常说的虚拟主机<br>server段里面可以配置各个站点特有的配置</p>
<p>  <span style="font-size:12px;">NGINX每个进程配置一个CPU<br>nginx进程设置方法, worker_processes 1;<br>查看CPU个数:<br>cat /proc/cpuinfo | grep processor<br>配置1:4 CPU (4 Core) + 4 worker_processes (每个worker_processes 使用1个CPU)<br>orker_processes 4;<br>orker_cpu_affinity 0001 0010 0100 1000;<br>配置2:8 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)<br>orker_processes 8;<br>orker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;<br>配置3:16 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)<br>orker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;</span></p>
<p>  <span style="font-size:12px;">基于域名的虚拟主机配置<br>1.两个站点分别为：Web1.ttlsa.com、Web2.ttlsa.com<br>搭建web1主目录、搭建web2主目录</span></p>
<h1 id="mkdir-nginx-www-web1-ttlsa-com"><a href="#mkdir-nginx-www-web1-ttlsa-com" class="headerlink" title="mkdir /nginx_www/web1.ttlsa.com"></a>mkdir /nginx_www/web1.ttlsa.com</h1><h1 id="echo-“web1-ttlsa-com”-gt-nginx-www-index-html"><a href="#echo-“web1-ttlsa-com”-gt-nginx-www-index-html" class="headerlink" title="echo “web1.ttlsa.com” &gt; /nginx_www/index.html"></a>echo “web1.ttlsa.com” &gt; /nginx_www/index.html</h1><h1 id="mkdir-nginx-www-nginx-www-web2-ttlsa-com"><a href="#mkdir-nginx-www-nginx-www-web2-ttlsa-com" class="headerlink" title="mkdir /nginx_www//nginx_www/web2.ttlsa.com"></a>mkdir /nginx_www//nginx_www/web2.ttlsa.com</h1><h1 id="echo-“web2-ttlsa-com”-gt-nginx-www-web2-ttlsa-com-index-html"><a href="#echo-“web2-ttlsa-com”-gt-nginx-www-web2-ttlsa-com-index-html" class="headerlink" title="echo “web2.ttlsa.com” &gt; /nginx_www/ web2.ttlsa.com/index.html"></a>echo “web2.ttlsa.com” &gt; /nginx_www/ web2.ttlsa.com/index.html</h1><p>在http配置段里增加如下配置：<br>server {<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html;<br>}<br>server {<br>listen 80;<br>server_name web2.ttlsa.com;<br>location / {<br>root /nginx_www/web2.ttlsa.com; # 网站根目录<br>index index.html index.htm; # 默认首页<br>}<br>error_page 404 /404.html; # 404错误页面<br>error_page 500 502 503 504 /50x.html;<br>}<br>2.开始做个本地测试，先要修改<a href="http://www.ttlsa.com/windows/" title="windows" target="_blank" rel="external">windows</a>下的hosts文件,加入如下配置<br>C:\Windows\System32\drivers\etc\hosts<br>192.168.1.203 web1.ttlsa.com<br>192.168.1.203 web2.ttlsa.com</p>
<p>  <span style="font-size:12px;">NGINX配置文件过期时间expires</span></p>
<h1 id="参数off禁止修改应答头中的”Expires”和”Cache-Control”。"><a href="#参数off禁止修改应答头中的”Expires”和”Cache-Control”。" class="headerlink" title="参数off禁止修改应答头中的”Expires”和”Cache-Control”。"></a>参数off禁止修改应答头中的”Expires”和”Cache-Control”。</h1><h1 id="注意：expires仅仅适用于200-204-301-302-和304应答"><a href="#注意：expires仅仅适用于200-204-301-302-和304应答" class="headerlink" title="注意：expires仅仅适用于200, 204, 301, 302,和304应答"></a>注意：expires仅仅适用于200, 204, 301, 302,和304应答</h1><p>1.根据文件类型配置(大部分情况下是这么配置的)<br>对图片，flash文件在浏览器本地缓存30天<br>expires on # 启用设置expire过期时间<br>location ~ .<em>.(gif|jpg|jpeg|png|bmp|swf)$<br>{<br>expires 30d;<br>}<br>对js，css文件在浏览器本地缓存1小时<br>location ~ .</em>.(js|css)$<br>{<br>expires 1h;<br>}<br>2.根据目录来设置<br>location ~ ^/(image|js|static|flash)/{<br>root /nginx_www/down; # 匹配这些url主目录都在down下,并且过期时间为30<br>expires 30d;<br>}</p>
<p>  <span style="font-size:12px;">NGINX配置禁止访问某类文件<br>1.使用break<br>location ~ .*.(exe|doc|rar)$<br>{<br>if (-f $request_filename) # 注意if和(之间有个空格<br>{<br>root /nginx/x; #当名字匹配时,把网站根目录替换成其他目录,使得用户无法下载<br>break; # 当名字匹配时,直接跳出不处理<br>}<br>}<br>范例一：假如说我有一个txt文件在web1.ttlsa.com上,我不让别人访问,那么怎么做呢?<br>现在目录下建一个txt文件</span></p>
<h1 id="vi-nginx-www-web1-ttlsa-com-a-txt"><a href="#vi-nginx-www-web1-ttlsa-com-a-txt" class="headerlink" title="vi /nginx_www/web1.ttlsa.com/a.txt"></a>vi /nginx_www/web1.ttlsa.com/a.txt</h1><p>最后修改一下nginx.conf配置,Server段配置段如下：<br>server{<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com;<br>index index.html index.htm;<br>}<br>error_page 404 /404.html;<br>location ~ .txt$ {<br>if (-f $request_filename)<br>{</p>
<p>#root /nginx/xxx;<br>break;<br>}<br>}<br>}<br>接着我们来测试访问,可以看到提示404 Not Found<br>2.使用deny<br>location ~ .*.(exe|doc|rar)$<br>{<br>root /nginx/x; #当名字匹配时,把网站根目录替换成其他目录,使得用户无法下载<br>deny all;<br>}</p>
<p>  <span style="font-size:12px;">禁止访问某个目录<br>location ~ ^/(admin)/ {<br>deny all; # 所有访问/admin目录的URL都被拒绝掉<br>}</span></p>
<p>  <span style="font-size:12px;">限制某些ip访问<br>location / {<br>deny 192.168.1.1;<br>deny 192.168.2.0/24;<br>allow all; # 上面呢两个被拒绝,允许其他所有的ip<br>}</span></p>
<p>  <span style="font-size:12px;">nginx下载连接数限制、速度限制<br>1.limit_zone<br>语法：limit_zone zone_name $variable memory_max_size<br>默认值：no<br>使用字段：http<br>指令描述会话状态存储区域。<br>会话的数目按照指定的变量来决定，它依赖于使用的变量大小和memory_max_size的值。<br>如下例：<br>limit_zone one $binary_remote_addr 10m;<br>客户端的地址将用于会话，注意$binary_remote_addr变量将替换$remote_addr而被使用。<br>$remote_addr 变量的值的长度可以是7到15字节，因此大小指定为32或64字节。<br>$binary_remote_addr 变量的值的长度总是4字节，大小总是32字节。<br>当会话状态储存区域为1M时理论上可以处理32000个会话，每个会话大小为32字节。<br>2.limit_conn<br>语法：limit_conn zone_name max_clients_per_ip<br>默认值：no<br>使用字段：http, server, location<br>指令指定一个会话的最大同时连接数，超过这个数字的请求将被返回”Service unavailable” (503)代码。<br>如下例：<br>limit_zone one $binary_remote_addr 10m; # 使用10MB来存储会话,可存32w个会话<br>Server{<br>Listen 80;<br>Server_name download.ttlsa.com;<br>Index index.html index.html index.<a href="http://www.ttlsa.com/php/" title="php" target="_blank" rel="external">php</a>;</span></p>
<p>#Zone limit<br>Location / {<br>limit_conn one 1; # 值允许一个连接<br>limit_rate 20k; # 一个连接最大20k速度<br>}<br>}<br>接下来我们测试一下效果，我在web1.ttlsa.com根目录下传了一个飞信的安装包，20多MB，看看速度是多少吧，接近20KB/秒.<br>这次把速度开到50KB，连接数还是1，配置段如下<br>limit_conn one 1;<br>limit_rate 50k;<br>连接数改成10，速率到50KB，速度应该达到250KB/秒左右了吧<br>limit_conn one 10;<br>limit_rate 50k;</p>
<p>  <span style="font-size:12px;">Nginx列出目录下的列表-目录索引<br>整个站点<br>location / {<br>root /nginx/web1.ttlsa.com<br>autoindex on;<br>}<br>也可以单个目录<br>location / {<br>root /nginx/web1.ttlsa.com/list; # 要索引的目录<br>autoindex on; # 打开索引<br>}<br>接下来看看整个列子，把web1.ttlsa.com的list索引目录列出,配置如下<br>server{<br>listen 80;<br>server_name web1.ttlsa.com;<br>location / {<br>root /nginx_www/web1.ttlsa.com;<br>index index.html index.htm;<br>}<br>error_page 404 /404.html;<br>location /list<br>{<br>root /nginx_www/web1.ttlsa.com;<br>autoindex on;<br>默认关闭状态<br>autoindex_exact_size off;<br>默认为on，显示出文件的确切大小，单位是bytes。<br>改为off后，显示出文件的大概大小，单位是kB或者MB或者GB<br>autoindex_localtime on;<br>默认为off，显示的文件时间为GMT时间。<br>改为on后，显示的文件时间为文件的服务器时间<br>}<br>}</span></p>
<p>  <span style="font-size:12px;">NGINX日志处理<br>设置一个计划任务，每天12点把access.log剪切到一个目录下，并且从命名为响应的名字</span></p>
<p>  <span style="font-size:12px;">NGINX忽略部分日志<br>location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$<br>{<br>access_log off; # 列出的这些图片格式不记录日志<br>}</span></p>
<p>  <span style="font-size:12px;">NGINX反向代理配置<br>nginx.conf配置文件：<br>user nobody nobody;<br>worker_processes 4;<br>error_log logs/error.log crit;<br>pid logs/nginx.pid;<br>worker_rlimit_nofile 65535;<br>events {<br>use epoll;<br>worker_connections 65535;<br>}<br>http {<br>server_tokens off;<br>include mime.types;<br>default_type application/octet-stream;<br>server_names_hash_bucket_size 128;<br>sendfile on;<br>tcp_nopush on;<br>tcp_nodelay on;<br>keepalive_timeout 65;<br>gzip on;<br>gzip_min_length 1k;<br>gzip_buffers 4 16k;<br>gzip_http_version 1.0;<br>gzip_comp_level 2;<br>gzip_types text/plain application/x-javascript text/css application/xml;<br>gzip_vary on;<br>upstream mysrv {<br>server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>upstream bench {<br>server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>upstream bbs {<br>server 192.168.1.5:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.6:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>include vhost/<em>.conf;<br>}<br>aaa_example_com.conf配置文件：<br>server<br>{<br>listen 80;<br>server_name aaa.example.com;<br>index index.php index.html index.htm index.shtml;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘$status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/aaa_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>; # 当访问aaa.example.com,默认解析转发到后端的mysrv<br>include proxy.conf;<br>}<br>location /bench/<br>{<br>proxy_pass <a href="http://bench" target="_blank" rel="external">http://bench</a>; #当访问/bench/转发到upstream配置的bench下<br>I nclude proxy.conf;<br>}<br>}<br>bbs_example_com.conf配置文件：<br>server<br>{<br>listen 80;<br>server_name bbs.example.com </em>.bbs.example.com;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘ $status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/bbs_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://bbs" target="_blank" rel="external">http://bbs</a>;<br>include proxy.conf;<br>}<br>}<br>proxy.conf配置文件：<br>proxy_redirect off;<br>proxy_set_header Host $host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>client_max_body_size 50m; # 允许客户端请求的最大单个文件字节数<br>client_body_buffer_size 256k; # 缓冲区代理缓冲客户端请求的最大字节数<br>proxy_connect_timeout 30; # 连接后端服务器超时时间<br>proxy_send_timeout 30; # 后端服务器发送数据超时时间,连接以及建立<br>proxy_read_timeout 60; # 后端服务器响应请求超时时间,从开始发送到接受完毕<br>proxy_buffer_size 4k; # 代理请求缓存区大小<br>proxy_buffers 4 32k;<br>proxy_busy_buffers_size 64k; #系统繁忙时可申请的proxy_buffers大小<br>proxy_temp_file_write_size 64k; #proxy缓存临时文件的大小<br>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;</span></p>
<h1 id="故障转移"><a href="#故障转移" class="headerlink" title="故障转移"></a>故障转移</h1><p>proxy_max_temp_file_size 128m;<br>proxy_set_header指令用于在向反向代理的后端web服务器发起请求时添加指定Header头信息，当后端web服务器上有多个基于域名的虚拟主机时，要通过添加Header头信息Host，来指定请求的域名，这样后端web服务器才能识别该反向代理访问请求由哪个虚拟主机来处理。</p>
<p>  <span style="font-size:12px;">Nginx缓存服务器配置</span></p>
<h1 id="wget-http-labs-frickle-com-files-ngx-cache-purge-1-3-tar-gz-清缓存模块"><a href="#wget-http-labs-frickle-com-files-ngx-cache-purge-1-3-tar-gz-清缓存模块" class="headerlink" title="wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz //清缓存模块"></a>wget <a href="http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz" target="_blank" rel="external">http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz</a> //清缓存模块</h1><h1 id="tar-zxvpf-ngx-cache-purge-1-3-tar-gz-C-software"><a href="#tar-zxvpf-ngx-cache-purge-1-3-tar-gz-C-software" class="headerlink" title="tar zxvpf ngx_cache_purge-1.3.tar.gz -C ../software/"></a>tar zxvpf ngx_cache_purge-1.3.tar.gz -C ../software/</h1><h1 id="cd-usr-local-src-software-nginx-1-0-2"><a href="#cd-usr-local-src-software-nginx-1-0-2" class="headerlink" title="cd /usr/local/src/software/nginx-1.0.2"></a>cd /usr/local/src/software/nginx-1.0.2</h1><h1 id="configure-–user-nobody-–group-nobody-–prefix-usr-local-nginx-1-0-2-–with-http-stub-status-module-–with-http-ssl-module-–add-module-ngx-cache-purge-1-3"><a href="#configure-–user-nobody-–group-nobody-–prefix-usr-local-nginx-1-0-2-–with-http-stub-status-module-–with-http-ssl-module-–add-module-ngx-cache-purge-1-3" class="headerlink" title="./configure –user=nobody –group=nobody –prefix=/usr/local/nginx-1.0.2 –with-http_stub_status_module –with-http_ssl_module –add-module=../ngx_cache_purge-1.3"></a>./configure –user=nobody –group=nobody –prefix=/usr/local/nginx-1.0.2 –with-http_stub_status_module –with-http_ssl_module –add-module=../ngx_cache_purge-1.3</h1><h1 id="mkdir-p-www-nginx-proxy-temp-path"><a href="#mkdir-p-www-nginx-proxy-temp-path" class="headerlink" title="mkdir -p /www/nginx/proxy_temp_path"></a>mkdir -p /www/nginx/proxy_temp_path</h1><h1 id="mkdir-p-www-nginx-proxy-cache-path"><a href="#mkdir-p-www-nginx-proxy-cache-path" class="headerlink" title="mkdir -p /www/nginx/proxy_cache_path"></a>mkdir -p /www/nginx/proxy_cache_path</h1><p>nginx.conf配置文件:<br>user nobody nobody;<br>worker_processes 4;<br>error_log logs/error.log crit;<br>pid logs/nginx.pid;<br>worker_rlimit_nofile 65535;<br>events {<br>use epoll;<br>worker_connections 65535;<br>}<br>http {<br>server_tokens off;<br>include mime.types;<br>default_type application/octet-stream;<br>server_names_hash_bucket_size 128;</p>
<p>  <span style="font-size:12px;">sendfile on;<br>tcp_nopush on;<br>tcp_nodelay on;<br>keepalive_timeout 65;<br>gzip on;<br>gzip_min_length 1k;<br>gzip_buffers 4 16k;<br>gzip_http_version 1.0;<br>gzip_comp_level 2;<br>gzip_types text/plain application/x-javascript text/css application/xml;<br>gzip_vary on;<br>upstream mysrv {<br>server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=30s;<br>server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;<br>}<br>include vhost/<em>.conf;<br>}<br>aaa_example_com.conf配置文件：<br>server {<br>listen 80;<br>server_name aaa.example.com;<br>index index.php index.html index.htm index.shtml;<br>log_format proxy ‘$remote_addr| $upstream_addr| $connection| $upstream_status| $time_local| $request|’<br>‘ $status| $body_bytes_sent| $bytes_sent| $http_referer|’<br>‘ $http_user_agent| $upstream_response_time| $msec| $request_time’;<br>access_log logs/aaa_access.log proxy;<br>location /<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>;<br>proxy_set_header Host $host;<br>proxy_set_header X-Forwarded-For $remote_addr;<br>}<br>location ~ .</em>.(gif|jpg|jpeg|png|bmp|swf|js|css)$<br>{<br>proxy_pass <a href="http://mysrv" target="_blank" rel="external">http://mysrv</a>;<br>include proxy.conf;<br>}<br>location ~ /purge(/.*)<br>{<br>allow 127.0.0.1;<br>allow 192.168.1.0/24;<br>deny all;<br>proxy_cache_purge cache_one $host$1$is_args$args;<br>}<br>}<br>proxy.conf配置文件：<br>proxy_redirect off;<br>proxy_set_header Host $host;<br>proxy_set_header X-Real-IP $remote_addr;<br>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>client_max_body_size 50m; //允许客户端请求的最大单个文件字节数<br>client_body_buffer_size 256k; //缓冲区代理缓冲客户端请求的最大字节数<br>proxy_connect_timeout 30; //连接后端服务器超时时间<br>proxy_send_timeout 30; //后端服务器发送数据超时时间<br>proxy_read_timeout 60; //后端服务器响应请求超时时间<br>proxy_buffer_size 4k; //代理请求缓存区大小<br>proxy_buffers 4 32k;<br>proxy_busy_buffers_size 64k; //系统繁忙时可申请的proxy_buffers大小<br>proxy_temp_file_write_size 64k; //proxy缓存临时文件的大小<br>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;<br>//故障转移<br>proxy_max_temp_file_size 128m;<br>proxy_temp_path /www/nginx/proxy_temp_path;<br>proxy_cache_path /www/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g; //设置web缓存区名称为cache_one，内存缓存空间为200m，自动清除超过1天没有被访问的缓存数据，硬盘缓存空间为1g<br>proxy_cache cache_one; //使用web缓存区cache_one<br>proxy_cache_valid 200 304 12h;<br>proxy_cache_valid 301 302 1m;<br>proxy_cache_valid any 1m;<br>proxy_cache_key $host$uri$is_args$args; //设置web缓存的key值，nginx根据key值md5哈希存储缓存</span></p>
<div><br><br><em> </em> *<br><br></div><br><div><br>  <span style="color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">文章标题: </span><a href="http://www.ttlsa.com/nginx/nginx-document/" target="_blank" rel="external">Linux运维文档之nginx</a><br><span style="color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;">原文地址: </span><a href="http://www.ttlsa.com/nginx/nginx-document/" title="Linux运维文档之nginx" target="_blank" rel="external">http://www.ttlsa.com/nginx/nginx-document/</a><br><br></div><br><div id="xunlei_com_thunder_helper_plugin_d462f475-c18e-46be-bd10-327458d045bd" style="margin: 0px; padding: 0px; color: rgb(85, 85, 85); font-family: 宋体, Arial, 'Microsoft Yahei', sans-serif; font-size: 14px; line-height: 20.2999992370605px;"><br></div>
      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/12/09/Linux运维文档之nginx【转】/" data-id="cimvrqg1v0002kyfea8egd46n" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/12/09/Linux运维文档之nginx【转】/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">
        
          记Fedora 21 Yun源配置和软件安装
        
      </div>
    </a>
  
  
    <a href="/2014/11/26/CentOS-7安装完第一步：yum源配置/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">CentOS 7安装完第一步：yum源配置</div>
    </a>
  
</nav>

  
</article>


  <section id="comments">
    <div id="ds-thread" class="ds-thread" data-thread-key="2014/12/09/Linux运维文档之nginx【转】/" data-title="Linux运维文档之nginx【转】" data-url="http://localhost/2014/12/09/Linux运维文档之nginx【转】/"></div>
  </section>
</section>
      
      <aside id="sidebar">
  
    
  
    
  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">十二月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">十一月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">七月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">六月 2014</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">近期文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/">Linux服务器配置与LNMP安装实录---(1)</a>
          </li>
        
          <li>
            <a href="/2015/04/16/科学上网，简易教程/">科学上网，简易教程</a>
          </li>
        
          <li>
            <a href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/">记Fedora 21 Yun源配置和软件安装</a>
          </li>
        
          <li>
            <a href="/2014/12/09/Linux运维文档之nginx【转】/">Linux运维文档之nginx【转】</a>
          </li>
        
          <li>
            <a href="/2014/11/26/CentOS-7安装完第一步：yum源配置/">CentOS 7安装完第一步：yum源配置</a>
          </li>
        
      </ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="http://xiguabaobao.com" target="_blank">主题作者</a>
          </li>
        
          <li>
            <a href="http://reqianduan.com" target="_blank">热前端</a>
          </li>
        
          <li>
            <a href="https://www.xiyoulinux.org" target="_blank">西邮linux兴趣小组</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 wangbo<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
      .
      Theme by <a href="https://github.com/xiangming/landscape-plus" target="_blank">Landscape-plus</a>
    </div>
  </div>
</footer>
  </div>
  <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
  <!-- totop start -->
<div id="totop">
<a title="返回顶部"><img src="/img/scrollup.png"/></a>
</div>

<!-- totop end -->

<!-- 多说公共js代码 start -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"idina"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0]
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
  </script>
<!-- 多说公共js代码 end -->


<!-- 百度分享 start -->

<!-- 百度分享 end -->

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>




<script src="/js/script.js"></script>

</div>
</body>
</html>
